/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2023-11-30
 * Time: 14:29
 */
public class My_SingleList implements Slist {
    static class ListNode {
        //存放数值
        public int value;
        //next是一个引用变量
        public ListNode next;
        //构造方法
        public ListNode(int value) {
            this.value = value;
        }
    }
    //head是一个标记的头节点的引用变量
    public ListNode head;
    public void createList() {
        //创建节点
        ListNode node1 = new ListNode(11);
        ListNode node2 = new ListNode(22);
        ListNode node3 = new ListNode(33);
        ListNode node4 = new ListNode(44);
        //连接节点
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }
    //打印链表
    @Override
    public void display() {
        //判断链表是否为空
        if(isEmpty()) {
            throw new EmptyException("链表为空");
        }
        //定义一个临时变量cur,让cur移动，避免head移动
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.value+" ");
            cur= cur.next;
        }
        System.out.println();
    }
    private boolean isEmpty() {
        return head == null;
    }
    //头插法
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        //如果没有节点,直接将head标记放在要插的节点上，作为第一个首节点
        if(head == null) {
            this.head = node;
        }
        else {
            //先绑定，再移标记引用变量
            node.next = this.head;
            this.head = node;
        }
    }
    //尾插法
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //如果没有节点,直接将head标记放在要插的节点上，作为第一个尾节点
        if(head == null) {
            this.head = node;
        }
        else {
            ListNode cur = head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }

    }
    //任意位置插入,第一个数据节点为0号下标
    @Override
    public void addIndex(int index, int data) {
        //判断index是否合法
        if(index < 0 || index > size()) {
            throw new IndexException("坐标"+index+":位置不合法");
        }
        ListNode node = new ListNode(data);
        //1.如果从头插入，直接头插法
        if(index == 0) {
            addFirst(data);
        }
        //2.从中间插
            ListNode cur = searchPrevIndex(index);
            node.next = cur.next;
            cur.next = node;
    }
    //创建一个找前驱的方法
    private ListNode searchPrevIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while(count < index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(key == cur.value) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    @Override
    public void remove(int key) {
        //删除头节点
        if(head.value == key) {
            head = head.next;
            return;
        }
        //1.找到要删除key的前驱
        ListNode cur = searchPrevKey(key);
        if(cur == null) {
            return;//没有你要删除的key
        }
        //2.定义一个标记curNext
        ListNode curNext = cur.next;
        //3.删除key
        cur.next = curNext.next;
    }
    private ListNode searchPrevKey(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.value == key) {
                return cur;
            }
            else {
                cur = cur.next;
            }
        }
        return null;
    }
    //删除所有值为key的节点  利用循环来删除，时间复杂度高(不建议)
    @Override
    public void removeAllKeyCirculate(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.value == key) {
                //1.找到要删除key的前驱
                 cur = searchPrevKey(key);
                if(cur == null) {
                    return;//没有你要删除的key
                }
                //2.定义一个标记curNext
                ListNode curNext = cur.next;
                //3.删除key
                cur.next = curNext.next;
            }
            else {
                cur = cur.next;
            }
        }
        if(head.value == key) {
            head = head.next;
        }
    }
    //删除所有值为key的节点(非循环方法)
    @Override
    public void removeAllKeye(int key) {
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null) {
            if(cur.value == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //删除头节点
        if(head.value == key) {
            head = head.next;
        }
    }

    //得到单链表的长度
    @Override
    public int size() {
        ListNode cur =head;
        int count = 0;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }
    //清空链表
    @Override
    public void clear() {
    head = null;
    }
}
